package com.zgkxzx.算法.大数处理;

/**
 * 创建者 zgkxzx
 * 日期   12/26/17
 * 描述
 */

public class BigNumber {

    /**
     * 大数相乘
     *
     * @param op1
     * @param op2
     * @return
     */
    public static String multiOperate(String op1, String op2) {
//        if (TextUtils.isEmpty(op1) || TextUtils.isEmpty(op2))
//            return "0";


        int length1 = op1.length();
        int length2 = op2.length();


        //char[] chars1 = op1.toCharArray();
        //char[] chars2 = op2.toCharArray();

        char[] chars1 = new StringBuffer(op1).reverse().toString().toCharArray();
        char[] chars2 = new StringBuffer(op2).reverse().toString().toCharArray();

        int[] result = new int[length1 + length2 + 2];

        for (int i = 0; i < length1; i++)
            for (int j = 0; j < length2; j++) {
                result[i + j] += Integer.valueOf(chars1[i] - '0') * Integer.valueOf(chars2[j] - '0');
            }

        for (int i = 0; i < result.length - 1; i++) {
            int nextValue = result[i] / 10;
            result[i] = result[i] % 10;
            if (nextValue > 0)
                result[i + 1] += nextValue;
        }


        StringBuilder stringBuilder = new StringBuilder();
        boolean isVaild = false;
        for (int i = result.length - 1; i >= 0; i--)
            if (result[i] != 0) {
                stringBuilder.append(result[i]);
                isVaild = true;
            } else if (result[i] == 0 && isVaild) {
                stringBuilder.append(result[i]);
            }


        return stringBuilder.toString();
    }

//    private void reversalCharArray(char[] chars1) {
//
//
//
//    }


}
